package net.contrend.util;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
 * <p>Copyright @ 2009 www.contrend.net</p>
 * <p>All right reserved.</p>
 * <p>项目名称 ： 进销存管理系统</p>
 * <p>JDK使用版本号 ： jdk 6.0</P>
 * <p>描述 ：</p>
 * @版本 ： 1.0.0
 * @author ： 谢庚才
 * <p>修改历史</p>
 * <p>修改时间		修改人员		修改内容</p>
 *    Nov 19, 2009 	谢庚才 		新建
 */
public class DataSourceManager {

    private static String DRIVER;
    private static String URL;
    private static String USER_NAME;
    private static String PASSWORD;

    /**
     * 初始化操作，初始化 DRIVER、URL、USER_NAME、PASSWORD
     */
    @SuppressWarnings("unused")
    private static void init() {

    }

    /**
     * Usually DataSource is got from JNDI, but for test purpose we generate a
     * simple DataSource implementation.
     * 
     * @return DataSource instance.
     */
    public static DataSource getDataSource() throws Exception {
        Class.forName(DRIVER);
        return new DataSource() {
            private int loginTimeout;
            private PrintWriter out;

            public Connection getConnection() throws SQLException {
                return getDirectConnection();
            }

            public Connection getConnection(String username, String password) throws SQLException {
                return getDirectConnection();
            }

            public PrintWriter getLogWriter() throws SQLException {
                return out;
            }

            public int getLoginTimeout() throws SQLException {
                return loginTimeout;
            }

            public void setLogWriter(PrintWriter out) throws SQLException {
                this.out = out;
            }

            public void setLoginTimeout(int seconds) throws SQLException {
                this.loginTimeout = seconds;
            }

            public boolean isWrapperFor(Class<?> iface) throws SQLException {
                return false;
            }

            public <T> T unwrap(Class<T> iface) throws SQLException {
                return null;
            }
        };
    }

    static Connection getDirectConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER_NAME, PASSWORD);
    }

    public static String getDRIVER() {
        return DRIVER;
    }

    public static void setDRIVER(String driver) {
        DRIVER = driver;
    }

    public static String getURL() {
        return URL;
    }

    public static void setURL(String url) {
        URL = url;
    }

    public static String getUSER_NAME() {
        return USER_NAME;
    }

    public static void setUSER_NAME(String user_name) {
        USER_NAME = user_name;
    }

    public static String getPASSWORD() {
        return PASSWORD;
    }

    public static void setPASSWORD(String password) {
        PASSWORD = password;
    }

}
